

clear all
use "[Folder]\Panel_Firm_Lab_OccLayer_temp.dta"



//Adjust by which lab quantity measure you use
	global MLab = 129
	global FLab = 2
	distinct Ind if u==0
	global Inds = r(ndistinct) - 1
	global Sizes = 4
	global Cities = 42
	distinct year if u==0
	global Years = r(ndistinct) - 1
	global Res = 13
	global NbrOfParam =  $MLab  + $FLab + $Cities + $Sizes + $Years + $Inds + $Res
	disp $NbrOfParam


//Function evaluation program
     program nlceslab
                version 14.2
                syntax varlist [if], at(name)
                local log_VA: word 1 of `varlist'
					
// Retrieve parameters out of at matrix				
				tempvar LabAgg0 LabAgg1 LabAgg2 LabAgg3 LabAgg4 temp_VA
				gen double `LabAgg0' = 0 `if'
				gen double `LabAgg1' = 0 `if'
				gen double `LabAgg2' = 0 `if'
				gen double `LabAgg3' = 0 `if'
				gen double `LabAgg4' = 0 `if'
                gen double `temp_VA' = 0 `if'
				
				replace `LabAgg0' = `LabAgg0' + RefGr0   `if'
				replace `LabAgg1' = `LabAgg1' + RefGr1   `if'
				replace `LabAgg2' = `LabAgg2' + RefGr2   `if'
				replace `LabAgg3' = `LabAgg3' + RefGr3   `if'
				replace `LabAgg4' = `LabAgg4' + RefGr4   `if'

				//Labor input units						
				forvalues i=2(1)$MLab {
				tempname delta 
				scalar `delta' = `at'[1,`i']
				replace `LabAgg0' = `LabAgg0' + `delta' * (lab_M0_`i')   `if'
				replace `LabAgg1' = `LabAgg1' + `delta' * (lab_M1_`i')   `if'
				replace `LabAgg2' = `LabAgg2' + `delta' * (lab_M2_`i')   `if'
				replace `LabAgg3' = `LabAgg3' + `delta' * (lab_M3_`i')   `if'
				replace `LabAgg4' = `LabAgg4' + `delta' * (lab_M4_`i')   `if'
				}
				
				tempname  delta 
				scalar `delta' = `at'[1,$MLab +1]
				replace `LabAgg0' = `LabAgg0' + `delta' * (lab_FNat0)  `if'
				replace `LabAgg1' = `LabAgg1' + `delta' * (lab_FNat1)  `if'
				replace `LabAgg2' = `LabAgg2' + `delta' * (lab_FNat2)  `if'
				replace `LabAgg3' = `LabAgg3' + `delta' * (lab_FNat3)  `if'
				replace `LabAgg4' = `LabAgg4' + `delta' * (lab_FNat4)  `if'
				
				
				tempname delta 
				scalar `delta' = `at'[1,$MLab +2]
				replace `LabAgg0' = `LabAgg0' + `delta' * (lab_FFor0)   `if'
				replace `LabAgg1' = `LabAgg1' + `delta' * (lab_FFor1)   `if'
				replace `LabAgg2' = `LabAgg2' + `delta' * (lab_FFor2)   `if'
				replace `LabAgg3' = `LabAgg3' + `delta' * (lab_FFor3)   `if'
				replace `LabAgg4' = `LabAgg4' + `delta' * (lab_FFor4)   `if'
					
				
	
				//Adj index for number of industry dummies
				forvalues i=1(1)$Inds {
				tempname Industry 
				scalar `Industry' = `at'[1,$MLab + $FLab + `i']
				replace `temp_VA' = `temp_VA' + `Industry'*Ind`i' 	`if'			
				}
				
				//Adj index for number of firm size dummies
				forvalues i=1(1)$Sizes {
				tempname Size 
				scalar `Size' = `at'[1, $MLab + $FLab  + $Inds +`i']
				replace `temp_VA' = `temp_VA' + `Size'*FirmSize_`i' `if'				
				}
				
				//Adj index for number of City dummies
				forvalues i=1(1)$Cities {
				tempname City 
				scalar `City' = `at'[1, $MLab  + $FLab  + $Inds + $Sizes +`i']
				replace `temp_VA' = `temp_VA' + `City'*k_`i' 	`if'			
				}
				//Adj index for number of year dummies
				forvalues i=1(1)$Years {
				tempname Year 
				scalar `Year' = `at'[1, $MLab  + $FLab  + $Inds + $Sizes +$Cities +`i']
				replace `temp_VA' = `temp_VA' + `Year'*y_`i' `if'
				}				


                
				// Now fill in dependent variable. Cut some higher-order interaction terms to reduce number of parameters (a bit of convergence issues). Trying the same for baseline indicates higher-order interactions make no difference at all. 

                tempname ic thetaL Cap Cap_Sq Cap_Qu TI TI_Sq TI_Qu TICap sigma beta_0 beta_1 beta_2 beta_4
				scalar `ic' = `at'[1,1]
				scalar `thetaL' = `at'[1,$MLab  + $FLab + $Inds + $Sizes +$Cities + $Years + 1]
				scalar `Cap' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 2]
				scalar `Cap_Sq' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 3]
				scalar `Cap_Qu' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 4]
				
				scalar `TI' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 5]
				scalar `TI_Sq' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 6]
				scalar `TI_Qu' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 7]
			
				scalar `TICap' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 8]
	
				scalar `sigma' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 9]
				scalar `beta_0' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 10]
				scalar `beta_1' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 11]
				scalar `beta_2' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 12]
				scalar `beta_4' = `at'[1,$MLab  + $FLab +  $Inds + $Sizes +$Cities + $Years + 13]

				
				replace `temp_VA' = `temp_VA' + `ic' + `thetaL' *(1/`sigma') *ln( `beta_0'*(`LabAgg0'^(`sigma')) +`beta_1'*(`LabAgg1'^(`sigma')) +`beta_2'*(`LabAgg2'^(`sigma') ) +`LabAgg3'^(`sigma')  +`beta_4'*(`LabAgg4'^(`sigma') )  ) +  `Cap'*logCap + `Cap_Sq'*logCap_Sq + `Cap_Qu'*logCap_Qu + `TI'*logGrosInvTot + `TI_Sq'*logGrosInvTot_Sq + `TI_Qu'*logGrosInvTot_Qu  + `TICap'*logGrosInvTot*logCap  `if'
				replace `log_VA' = `temp_VA'   `if'
            end
			
			
		
			
mat M = J(1,$NbrOfParam,1)

cd "[Output Folder]\"
//Est. for upper half of non-routinization industries
nl ceslab @ logFtg_Foradlingsvarde if u==0 , nparameters($NbrOfParam) initial(M)
parmest, format(estimate min95 max95 %8.2f p %8.1e) saving(HC_OccLayer, replace) omit empty 

use "[Output Folder]\HC_OccLayer.dta", clear


drop parm
gen par_nr= regexr(eq,"b","")
destring par_nr, replace
replace par_nr = 0 if par_nr ==1
replace par_nr = 1 if par_nr ==107
replace par_nr = 300 if par_nr ==130
replace par_nr = 301 if par_nr ==131
keep if par_nr<130 

rename par_nr FodelseLandnamn
drop if stderr==0
rename estimate HC
keep  HC  FodelseLandnamn


merge 1:1 FodelseLandnamn using "[Folder]\MacroData_ToMona_v1"
drop if _merge==2
drop _merge

merge 1:1 FodelseLandnamn using "[Output Folder]\Weights_Tot"
drop if _merge==2
drop _merge


reg HC TR SS [aw=Weights] if WVInd==1 & cognitive!=. 

reg HC TR SS LifeExp FertRate cognitive [aw=Weights] if WVInd==1 & cognitive!=. 


///////////////////////////////////////////////////////////////
